//
//  SepcialDetailViewController.m
//  CompareToBuy
//
//  Created by qianfeng on 15/12/17.
//  Copyright © 2015年 qianfeng. All rights reserved.
//

#import "SepcialDetailViewController.h"
#import "TxcModel.h"
#import "AFNetworking.h"
#import "TxcPrefixHeader.pch"
#import "OverseasOnlineShopping-Swift.h"
#import "MJRefreshHeader.h"
#import "MJRefreshFooter.h"
#import "MJRefresh.h"
//#import "SpecialDetail2ViewController.h"

@interface SepcialDetailViewController ()
{
    UITableView *_tableView;
    NSMutableArray *_dataArray;
    NSMutableDictionary *_parametersDict;
    NSInteger _pageIndex;
    BOOL _isPulling;
}
@end

@implementation SepcialDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.title = @"精彩专题";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [[NSMutableArray alloc] init];
    _pageIndex = 1;
    [self createData];
    [self createTableView];
    [self createRefresh];
}

- (void)createData {
    if (_isPulling) {
        [_dataArray removeAllObjects];
    }
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *page = [NSString stringWithFormat:@"%lu", _pageIndex];
    NSDictionary *parametersDict = @{@"p_btSpecialType": @"0",
                                       @"p_iPageIndex": page,
                                       @"p_iPageSize": @"20",
                                       @"p_iParentSpecialID": @"0"
                                       };
    [manager POST:BUTTON1_URL parameters: parametersDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dict in array) {
            TxcModel *model = [TxcModel modelWithDict:dict];
            [_dataArray addObject:model];
        }
        [_tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)createTableView {
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 240;
    _tableView.backgroundColor = BACKCOLOR;
    _tableView.separatorStyle = UITableViewCellAccessoryNone;
    [self.view addSubview:_tableView];
    UINib *nib = [UINib nibWithNibName:@"SpecialDetailCell" bundle:[NSBundle mainBundle]];
    [_tableView registerNib:nib forCellReuseIdentifier:@"specialDetailCell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [SpecialDetailCell specialCellWith:tableView indexPath:indexPath dataArray:_dataArray];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TxcModel *model = _dataArray[indexPath.row];
    SpecialCollectionViewController *specialCollectionVC = [[SpecialCollectionViewController alloc] init];
    specialCollectionVC.specialID = [NSString stringWithFormat:@"%@", model.iSpecialID];
    [self.navigationController pushViewController:specialCollectionVC animated:YES];
}

- (void)createRefresh {
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    _tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
}

- (void)downRefresh {
    _isPulling = YES;
    [_tableView.mj_header beginRefreshing];
    [self createData];
    [_tableView.mj_header endRefreshing];
}

- (void)upRefresh {
    _isPulling = NO;
    _pageIndex ++;
    [_tableView.mj_footer beginRefreshing];
    [self createData];
    [_tableView.mj_footer endRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
